package com.itheima12;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;

/**
 * @author Xiao
 * @date 2021/7/21
 */
public class PeriodDemo {
    /**
     * Period
     */
    public static void main(String[] args) {
        LocalDate date1 = LocalDate.of(2000,01,01);
        LocalDate date2 = LocalDate.of(2020, 12, 12);
        Period p = Period.between(date1,date2);
        System.out.println(p);  //P20Y11M11D →
        //P:时间间隔对象  20Y11M11D:时间间隔20年11个月11天
        System.out.println(p.getYears());   //20:获取年间隔
        System.out.println(p.getMonths());  //11:获取月间隔
        System.out.println(p.getDays());    //11:获取日间隔
        System.out.println(p.toTotalMonths());  //251:获取月间隔总数
        System.out.println("--------");
        LocalDateTime time1 = LocalDateTime.of(2020, 1, 1, 10, 10, 10);
        LocalDateTime time2 = LocalDateTime.of(2021, 2, 2, 11, 11, 11);
        Duration d = Duration.between(time1,time2);
        System.out.println(d);  //PT9553H1M1S：9553小时1分1秒
        System.out.println(d.toSeconds());  //34390861：秒
        System.out.println(d.toMillis());   //34390861000：豪秒
        System.out.println(d.toNanos());    //34390861000000000：纳秒
    }
}
